package com.jsgt.greekfire.ui.pool.incomerecord.adapter

import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.fhx.core.utils.DateFormatUtil
import com.fhx.core.utils.PriceFormatUtil
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.IncomeRecordBean
import java.lang.Exception

/**
 * 收益记录
 **/
class IncomeRecordAdapter(var isSuper: Boolean) : BaseAdapter<IncomeRecordBean>() {

    inner class VH(itemView: View) : BaseViewHolder(itemView) {

        private var timeTv: TextView? = null
        private var incomePersonalTv: TextView? = null
        private var incomeMarketTv: TextView? = null
        private var incomeSuperNodeTv: TextView? = null
        private var incomeRlSuper: RelativeLayout? = null

        init {
            timeTv = itemView.findViewById(R.id.time_tv)
            incomePersonalTv = itemView.findViewById(R.id.income_personal_tv)
            incomeMarketTv = itemView.findViewById(R.id.income_market_tv)
            incomeSuperNodeTv = itemView.findViewById(R.id.income_super_node_tv)
            incomeRlSuper = itemView.findViewById(R.id.income_rl_super)
        }


        fun bindData(bean: IncomeRecordBean) {
            incomePersonalTv?.text =
                "${if (bean.singleAward == null) 0 else PriceFormatUtil.format(
                    bean.singleAward!!,
                    4
                )}"
            incomeMarketTv?.text =
                "${if (bean.teamAward == null) 0 else PriceFormatUtil.format(bean.teamAward!!, 4)}"

            if (isSuper) {
                incomeRlSuper?.visibility = View.VISIBLE
                incomeSuperNodeTv?.text =
                    "${if (bean.superAward == null) 0 else PriceFormatUtil.format(
                        bean.superAward!!,
                        4
                    )}"
            } else {
                incomeRlSuper?.visibility = View.GONE
            }

            val time = try {
                DateFormatUtil.formatNew(bean.createTime, "yyyy-MM-dd HH:mm")
            } catch (e: Exception) {
                bean.createTime
            }
            val timeText = "$time"
            val ss = SpannableString(timeText)
            val fcs =
                ForegroundColorSpan(incomePersonalTv?.context?.resources?.getColor(R.color.gray_A4A4A4)!!)
            ss.setSpan(fcs, 5, timeText.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
            timeTv?.text = ss
        }
    }

    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_income, parent, false)
        return VH(view)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VH
        viewHolder.bindData(dataList()[position])
    }
}